package co.fitstart.market.web.controller.course;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.market.MarketBatchCourseware;
import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.market.MarketDailyCourseware;
import co.fitstart.entity.market.ParticipationQrcode;
import co.fitstart.entity.market.TrainingRecord;
import co.fitstart.entity.market.template.MarketCoursewareTemplate;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.CoursewareService;
import co.fitstart.market.service.ParticipationService;
import co.fitstart.market.service.QrcodeService;
import co.fitstart.market.service.UserService;
import co.fitstart.market.web.interceptor.MobileDetect;
import co.fitstart.market.web.interceptor.ParticipateDetect;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;
import co.fitstart.market.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */

@Controller
@RequestMapping("/courses/{monthlyId}/batches/{courseId}/units")
public class CourseUnitController {
    
    @Autowired
    private CoursewareService coursewareService;
    
    @Autowired
    private QrcodeService qrcodeService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ParticipationService participationService;
    
    /**
     * course unit page
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView index(@Secure User user, @PathVariable("courseId") Long courseId) {
        
        ModelAndView modelAndView = new ModelAndView("course/unit/unit-index");
        
        MarketBatchCourseware batchCourseware = coursewareService.loadBatchCourseware(courseId);
        modelAndView.addObject("batchCourseware", batchCourseware);
        
        TrainingRecord record = coursewareService.getTrainRecord(user, new Date());
        modelAndView.addObject("record", record);
        
        MarketDailyCourseware dailyCourseware = null;
        
        if(record == null) {
            dailyCourseware = coursewareService.loadDailyCourseware(batchCourseware);
        } else {
            dailyCourseware = coursewareService.loadDailyCourseware(record.getDailyId());
        }
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * course unit training page
     * 
     * @param unitId
     * @return
     */
    @RequestMapping(value = "/{unitId}/training", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView training(@Secure User user, @PathVariable("unitId") Long unitId) {
        
        ModelAndView modelAndView = new ModelAndView("course/unit/unit-training");
        
        MarketDailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(unitId);
        
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * course unit homework page
     * 
     * @param unitId
     * @return
     */
    @RequestMapping(value = "/{unitId}/homework", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView homework(@Secure User user, @PathVariable("unitId") Long unitId) {
        
        ModelAndView modelAndView = new ModelAndView("course/unit/unit-homework");
        
        MarketDailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(unitId);
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        
        List<MarketCoursewareTemplate> coursewares = dailyCourseware.getTemplate().getCoursewares();
        modelAndView.addObject("coursewares", coursewares);
        
        modelAndView.addObject("coursewareAmount", coursewares.size());
        
        modelAndView.addObject("user", user);
        
        TrainingRecord record = coursewareService.getTrainRecord(user, new Date());
        modelAndView.addObject("record", record);
        
        return modelAndView;
    }
    
    /**
     * course unit training result page
     * 
     * @param unitId
     * @return
     */
    @RequestMapping(value = "/{unitId}/result", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    @WechatJsConfig
    public ModelAndView result(@Secure User user, @PathVariable("unitId") Long unitId) {
        
        ModelAndView modelAndView = new ModelAndView("course/unit/unit-result");
        
        modelAndView.addObject("user", user);
        
        MarketDailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(unitId);
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        modelAndView.addObject("batchCourseware", dailyCourseware.getBatch());
        
        MarketCourseRound courseRound = dailyCourseware.getBatch().getMonthly().getCourseRound();
        modelAndView.addObject("courseRound", courseRound);
        
        modelAndView.addObject("unitId", unitId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param unitId
     * @param openid
     * @return
     */
    @RequestMapping(value = "/{unitId}/result-share", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    public ModelAndView share(@PathVariable("unitId") Long unitId, @RequestParam("inviter") String openid) {
        
        ModelAndView modelAndView = new ModelAndView("course/unit/unit-result-share");
        
        User inviter = userService.getUserByOpenId(openid);
        
        modelAndView.addObject("user", inviter);
        
        MarketDailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(unitId);
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        modelAndView.addObject("batchCourseware", dailyCourseware.getBatch());
        
        MarketCourseRound courseRound = dailyCourseware.getBatch().getMonthly().getCourseRound();
        modelAndView.addObject("courseRound", courseRound);
        
        modelAndView.addObject("unitId", unitId);
        
        ParticipationQrcode participationQrcode = participationService.loadParticipationQrcode(true, inviter);
        String qrcode = null;
        if(participationQrcode == null) {
            qrcode = qrcodeService.generateQrcode(true, inviter);
        } else {
            qrcode = participationQrcode.getQrcode();
        }
        modelAndView.addObject("qrcode", qrcode);
        
        return modelAndView;
    }
    
}
